﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace eproject_BusTicketOnline
{
    public partial class EditTicket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string busid = ddlBusId.SelectedValue;
                string TicketID = Session["TicketID"].ToString();
                lblBuslicketId.Text = TicketID;
                SqlConnection con2 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                con2.Open();
                //display Old information of ticket
                SqlCommand cmdOldBusId = new SqlCommand("Select BuslistId From BusticketDetail where BusticketDetailId='"+TicketID+"'",con2);
                SqlDataReader drOldBusId;
                drOldBusId = cmdOldBusId.ExecuteReader();
                while (drOldBusId.Read())
                {
                    lblOldBusId.Text = drOldBusId["BuslistId"].ToString();
                }
                con2.Close();
                con2.Open();
                SqlCommand cmdFullName = new SqlCommand("SELECT Member.FullName FROM Member INNER JOIN BusticKetDetail ON Member.MemberId = BusticKetDetail.MemberId where BusticketDetailId='" + TicketID + "'", con2);
                SqlDataReader drOldFullName;
                drOldFullName = cmdFullName.ExecuteReader();
                while (drOldFullName.Read())
                {
                    lblOldPayer.Text = drOldFullName["FullName"].ToString();
                }
                con2.Close();
                con2.Open();
                SqlCommand cmd = new SqlCommand("SELECT FullName From BusticketDetail where BusticketDetailId='" + TicketID + "'", con2);
                SqlDataReader drOldPassengerName;
                drOldPassengerName = cmd.ExecuteReader();
                while (drOldPassengerName.Read())
                {
                    lblOldPassenger.Text = drOldPassengerName["FullName"].ToString();
                }
                con2.Close();
                con2.Open();
                SqlCommand cmdOldAge = new SqlCommand("SELECT Age From BusticketDetail where BusticketDetailId='" + TicketID + "'", con2);
                SqlDataReader drOldAge;
                drOldAge = cmdOldAge.ExecuteReader();
                while (drOldAge.Read())
                {
                    lblOldAgePasssenger.Text = drOldAge["Age"].ToString();
                }
                con2.Close();


                //update ticket
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                con.Open();
                SqlDataAdapter daBUS = new SqlDataAdapter("SELECT BuslistId FROM Buslist", con);
                DataSet dsBus = new DataSet();
                daBUS.Fill(dsBus);
                ddlBusId.DataSource = dsBus;
                ddlBusId.DataTextField = "BuslistId";
                ddlBusId.DataBind();
               // SqlDataAdapter daPayer = new SqlDataAdapter("SELECT     dbo.Member.FullName FROM         dbo.Member INNER JOIN                      dbo.BusticKetDetail ON dbo.Member.MemberId = dbo.BusticKetDetail.MemberId", con);
                SqlDataAdapter daPayer = new SqlDataAdapter("SELECT Member.FullName FROM Member ", con);
                DataSet dsPayer = new DataSet();
                daPayer.Fill(dsPayer);
                ddlFullname.DataSource = dsPayer;
                ddlFullname.DataTextField = "Fullname";
                ddlFullname.DataBind();
                con.Close();
            }
        }

       

        protected void ddlBusId_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
            string busid = ddlBusId.SelectedValue;
            con.Open();
            SqlCommand cmdDPlace = new SqlCommand("SELECT DeparturePlace From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drDPlace;
            drDPlace = cmdDPlace.ExecuteReader();
            while (drDPlace.Read())
            {
                lblDeparturePlace.Text = drDPlace["DeparturePlace"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdAPlace = new SqlCommand("SELECT ArrivePlace From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drAPlace;
            drAPlace = cmdAPlace.ExecuteReader();
            while (drAPlace.Read())
            {
                lblArrivePlace.Text = drAPlace["ArrivePlace"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdStartDay = new SqlCommand("SELECT CONVERT(varchar(20),[StartDate],101) as StartDate From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drStartDay;
            drStartDay = cmdStartDay.ExecuteReader();
            while (drStartDay.Read())
            {
                lblStartDay.Text = drStartDay["StartDate"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdEndDay = new SqlCommand("SELECT CONVERT(varchar(20),[EndDate],101) as EndDate From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drEndDay;
            drEndDay = cmdEndDay.ExecuteReader();
            while (drEndDay.Read())
            {
                lblEndDay.Text = drEndDay["EndDate"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdTime = new SqlCommand("SELECT Time From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drTime;
            drTime = cmdTime.ExecuteReader();
            while (drTime.Read())
            {
                lblTime.Text = drTime["Time"].ToString();
            }
            con.Close();

            con.Open();
            SqlCommand cmdPrice = new SqlCommand("SELECT Price From Buslist where BusListId='" + busid + "'", con);
            SqlDataReader drPrice;
            drPrice = cmdPrice.ExecuteReader();
            while (drPrice.Read())
            {
                lblPrice.Text = drPrice["Price"].ToString();
            }
            con.Close();
            Page.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string TicketID = Session["TicketID"].ToString();
            string busid = ddlBusId.SelectedValue;
            string payer = ddlFullname.SelectedValue;
            string passenger = txtFullName.Text;
            string age = ddlAge.SelectedValue;
            Double price=Double.Parse(lblPrice.Text);
            // int price = Convert.ToInt32(lblPrice.Text);
            Double lastPrice=0;
            if (age == "Less 5 year old")
            {
                lastPrice = price * 0;
            }
            else
            if (age == "From 5 to 12")
            {
                lastPrice = price / 2;
            }
            else
            if (age == "From 13 to 50")
            {
                lastPrice = price;
            }
            else
            if (age == "Above 50")
            {
                lastPrice = price - (price*30/100);
            }
            SqlConnection con1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
            con1.Open();
            // get memberid with Fullname 
            SqlCommand cmdFullName = new SqlCommand("SELECT Member.MemberID FROM Member where Member.FullName='" + payer + "'", con1);
            SqlDataReader drFullName;
            drFullName = cmdFullName.ExecuteReader();
            while (drFullName.Read())
            {
                lblMemberId.Text = drFullName["MemberId"].ToString();
            }
            con1.Close();
            //
            
            SqlConnection con5 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
            con5.Open();
            SqlCommand cmd = new SqlCommand("UPDATE BusTicketDetail SET [MemberId]='" + lblMemberId.Text + "',[BusListId]='"+busid+"',[FullName]='"+txtFullName.Text+"',[Age]='"+ddlAge.SelectedValue+"',[DeparturePlace]='"+lblDeparturePlace.Text+"',[ArrivePlace]='"+lblArrivePlace.Text+"',[StartDate]='"+lblStartDay.Text+"',[EndDate]='"+lblEndDay.Text+"',[Time]='"+lblTime.Text+"',[Price]="+(lastPrice)+",TicketStatus='active' WHERE BusTicketDetailId='"+TicketID+"'", con5);
            cmd.ExecuteNonQuery();
            con5.Close();
            Response.Redirect("TicketManage.aspx");
        }

        protected void btnDeactive_Click(object sender, EventArgs e)
        {
            string TicketID = Session["TicketID"].ToString();
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("UPDATE BusticketDetail set TicketStatus='deactive' where BusticketDetailId='"+TicketID+"'", con);
            cmd.ExecuteNonQuery();
            Response.Redirect("TicketManage.aspx");
        }
    }
}